package fina2.dcs.security;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;


@Stateless
@Scope(ScopeType.SESSION)
@Name("rolePermSession")
@LocalBinding(jndiBinding = "RolePermissionSession/local")
@AutoCreate
public class RolePermissionSession implements RolePermissionLocal,Serializable {
	@PersistenceContext
	private EntityManager em;
	

	private Logger log=Logger.getLogger(getClass());

	public List<Permission> loadPermissionsByRoleId(BigDecimal roleId) {
		
		List<Permission> perms = new ArrayList<Permission>();
		String sql="SELECT p FROM "+Permission.class.getName()+" p WHERE p.id IN(SELECT r.rolePermPk.permissionId FROM " + RolePermission.class.getName() + " r WHERE r.rolePermPk.roleId=:roleId)";
		try {
			Query q = em.createQuery(sql);
			q.setParameter("roleId", roleId);
			perms=q.getResultList();
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return perms;
	}

	@Remove
	public void remove() {

	}
}
